package com.wontube.client.conf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.wontube.client.json.JsonReader;
import com.wontube.client.log.MinLoggerFactory;
import com.wontube.client.log.MinLoggerFactory.MinLogger;

/**
 * @author yuechao
 * 
 */
public final class ServerEnvironment {
	private static final MinLogger logger = MinLoggerFactory.getLogger(ServerEnvironment.class);
	public static final ServerEnvironment environment = new ServerEnvironment();
	
	/**
	 * session id, 用于单机客户端
	 */
	public static String lastClientSessionId = "";
	
	// 框架代码包前缀，不要修改
	public static final String coreClientPackagePrefix = "com.wontube.";
	// 业务代码包前缀，系统初始化时根据实际情况修改
	public static String clientPackagePrefix = "com.wontube.";
	public static final String charsetName = "UTF-8";
	
	private ServerEnvironment() {
	}

	private String serverUrl = "http://localhost:8080/server";
	private String defaultDataFormat = "java";
	private String defaultApiVersion = "1.4";
	private int defaultConnectTimeout = 20000;
	private int defaultReadTimeout = 30000;
	private String defaultAgent = "android";

	/**
	 * 获取服务器地址，最后不含/
	 * 
	 * @return
	 */
	public String getServerUrl() {
		return this.serverUrl;
	}

	/**
	 * 设置服务器地址，最后不含/
	 * 
	 * @param url
	 */
	public void setServerUrl(String url) {
		this.serverUrl = url;
	}

	public String getRemoteUrl() {
		return this.serverUrl + "/main.handler";
	}

	public String getDefaultDataFormat() {
		return this.defaultDataFormat;
	}

	public void setDefaultDataFormat(String defaultDataFormat) {
		this.defaultDataFormat = defaultDataFormat;
	}

	public String getDefaultApiVersion() {
		return this.defaultApiVersion;
	}

	public void setDefaultApiVersion(String defaultApiVersion) {
		this.defaultApiVersion = defaultApiVersion;
	}

	public int getDefaultConnectTimeout() {
		return this.defaultConnectTimeout;
	}

	public void setDefaultConnectTimeout(int defaultConnectTimeout) {
		this.defaultConnectTimeout = defaultConnectTimeout;
	}

	public int getDefaultReadTimeout() {
		return this.defaultReadTimeout;
	}

	public void setDefaultReadTimeout(int defaultReadTimeout) {
		this.defaultReadTimeout = defaultReadTimeout;
	}
	
	public String getDefaultAgent() {
		return this.defaultAgent;
	}

	public void setDefaultAgent(String defaultAgent) {
		this.defaultAgent = defaultAgent;
	}

	public static String processInterfaceName(String clazzName) {
		if (clazzName.startsWith(ServerEnvironment.clientPackagePrefix)) {
			clazzName = clazzName.substring(ServerEnvironment.clientPackagePrefix.length());
		} else if (clazzName.startsWith(ServerEnvironment.coreClientPackagePrefix)) {
			clazzName = clazzName.substring(ServerEnvironment.coreClientPackagePrefix.length());
		}
		return clazzName;
	}
	
	private void readConf(JsonReader reader) throws IOException {
		reader.beginObject();
		while (reader.hasNext()) {
			String name = reader.nextName();
			if ("serverUrl".equals(name)) {
				String value = reader.nextString();
				if (value != null && !"".equals(value)) {
					this.serverUrl = value;
				}
			} else if ("defaultApiVersion".equals(name)) {
				String value = reader.nextString();
				if (value != null && !"".equals(value)) {
					this.defaultApiVersion = value;
				}
			} else if ("defaultDataFormat".equals(name)) {
				String value = reader.nextString();
				if (value != null && !"".equals(value)) {
					this.defaultDataFormat = value;
				}
			} else if ("defaultConnectTimeout".equals(name)) {
				int value = reader.nextInt();
				if (value > 0) {
					this.defaultConnectTimeout = value;
				}
			} else if ("defaultReadTimeout".equals(name)) {
				int value = reader.nextInt();
				if (value > 0) {
					this.defaultReadTimeout = value;
				}
			} else if ("defaultAgent".equals(name)) {
				String value = reader.nextString();
				if (value != null && !"".equals(value)) {
					this.defaultAgent = value;
				}
			} else {
				reader.skipValue();
			}
		}
		reader.endObject();
	}

	public boolean load(String confPath) {
		JsonReader reader = null;
		try {
			reader = new JsonReader(new FileReader(new File(confPath)));
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage());
		}

		if (reader != null) {
			try {
				this.readConf(reader);
			} catch (IOException e) {
				logger.error(e.getMessage());
			} finally {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}

		if (this.serverUrl.endsWith("/")) {
			this.serverUrl = this.serverUrl.substring(0, this.serverUrl.length() - 1);
		}

		if (logger.isInfoEnabled()) {
			String info = String.format("loaded server config, api url = %s, defaultDataFormat=%s", this.serverUrl, this.defaultDataFormat);
			logger.info(info);
		}

		return true;
	}
}
